﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using System.Collections.Generic;//Please add references
namespace ElingOA6_Admin_DSJCJ.DAL
{
    /// <summary>
    /// 数据访问类:CdTaskClass
    /// </summary>
    public partial class CdTaskClass
    {
        /// <summary>
        /// 重点督查（文件阅办）类别定义 
        /// </summary>
        public CdTaskClass()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FTaskClassCd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from dbo.t_cdTaskClass");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)			};
            parameters[0].Value = FTaskClassCd;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取权限
        /// </summary>
        /// <param name="FTaskClassCd">编号</param>
        /// <returns>List</returns>
        public List<string> GetLoginIDListByTaskClassCd(string FTaskClassCd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FLogID ");
            strSql.Append(" FROM dbo.t_TaskAuthority ");
            strSql.Append(" WHERE FTaskClassCd=@FTaskClassCd");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassCd", SqlDbType.Decimal)
                                           };
            parameters[0].Value = FTaskClassCd;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            List<string> loginIDList = new List<string>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                loginIDList.Add(ds.Tables[0].Rows[i]["FLogID"].ToString());
            }

            return loginIDList;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ElingOA6_Admin_DSJCJ.Model.CdTaskClass model, List<Model.TaskAuthority> TaskAuthorityList)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("insert into dbo.t_cdTaskClass(");
            strSql.Append("FTaskClassCd,FTaskClassName,FScore)");
            strSql.Append(" values (");
            strSql.Append("@FTaskClassCd,@FTaskClassName,@FScore)");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FTaskClassName", SqlDbType.NVarChar,20),
					new SqlParameter("@FScore", SqlDbType.Decimal,5)};
            parameters[0].Value = model.FTaskClassCd;
            parameters[1].Value = model.FTaskClassName;
            parameters[2].Value = model.FScore;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            foreach (Model.TaskAuthority Model in TaskAuthorityList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_TaskAuthority(");
                strSql.Append("FTaskClassCd,FLogID,FEntityNo,FAuthorityType)");
                strSql.Append(" values (");
                strSql.Append("@FTaskClassCd,@FLogID,@FEntityNo,@FAuthorityType)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FAuthorityType", SqlDbType.NVarChar,20)};
                parameters2[0].Value = model.FTaskClassCd;
                parameters2[1].Value = Model.FLogID;
                parameters2[2].Value = Model.FEntityNo;
                parameters2[3].Value = Model.FAuthorityType;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(ElingOA6_Admin_DSJCJ.Model.CdTaskClass model, List<Model.TaskAuthority> TaskAuthorityList)
        {
            StringBuilder strSql = new StringBuilder();
            List<CommandInfo> cmdList = new List<CommandInfo>();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("update dbo.t_cdTaskClass set ");
            strSql.Append("FTaskClassName=@FTaskClassName,");
            strSql.Append("FScore=@FScore");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassName", SqlDbType.NVarChar,20),
					new SqlParameter("@FScore", SqlDbType.Decimal,5),
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)};
            parameters[0].Value = model.FTaskClassName;
            parameters[1].Value = model.FScore;
            parameters[2].Value = model.FTaskClassCd;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskAuthority ");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)			};
            parameters3[0].Value = model.FTaskClassCd;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            foreach (Model.TaskAuthority Model in TaskAuthorityList)
            {
                cmds = new CommandInfo();
                strSql = new StringBuilder();
                strSql.Append("insert into dbo.t_TaskAuthority(");
                strSql.Append("FTaskClassCd,FLogID,FEntityNo,FAuthorityType)");
                strSql.Append(" values (");
                strSql.Append("@FTaskClassCd,@FLogID,@FEntityNo,@FAuthorityType)");
                SqlParameter[] parameters2 = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3),
					new SqlParameter("@FLogID", SqlDbType.NVarChar,30),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,12),
					new SqlParameter("@FAuthorityType", SqlDbType.NVarChar,20)};
                parameters2[0].Value = model.FTaskClassCd;
                parameters2[1].Value = Model.FLogID;
                parameters2[2].Value = Model.FEntityNo;
                parameters2[3].Value = Model.FAuthorityType;
                cmds.CommandText = strSql.ToString();
                cmds.Parameters = parameters2;
                cmdList.Add(cmds);
            }


            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FTaskClassCd)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("delete from dbo.t_cdTaskClass ");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)			};
            parameters[0].Value = FTaskClassCd;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters;
            cmdList.Add(cmds);

            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskAuthority ");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters3 = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)			};
            parameters3[0].Value = FTaskClassCd;
            cmds.CommandText = strSql.ToString();
            cmds.Parameters = parameters3;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public void DeleteList(string FTaskClassCdlist)
        {
            List<CommandInfo> cmdList = new List<CommandInfo>();
            StringBuilder strSql = new StringBuilder();
            CommandInfo cmds = new CommandInfo();
            strSql.Append("delete from dbo.t_cdTaskClass ");
            strSql.Append(" where FTaskClassCd in (" + FTaskClassCdlist + ")  ");

            cmds.CommandText = strSql.ToString();
            SqlParameter[] abc = { new SqlParameter() };
            cmds.Parameters = abc;
            cmdList.Add(cmds);


            cmds = new CommandInfo();
            strSql = new StringBuilder();
            strSql.Append("delete from dbo.t_TaskAuthority ");
            strSql.Append(" where  FTaskClassCd in (" + FTaskClassCdlist + ")  ");
            cmds.CommandText = strSql.ToString();
            SqlParameter[] abd = { new SqlParameter() };
            cmds.Parameters = abd;
            cmdList.Add(cmds);

            if (cmdList.Count > 0)
            {
                DbHelperSQL.ExecuteSqlTranWithString(cmdList, 1800);
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.CdTaskClass GetModel(string FTaskClassCd)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FTaskClassCd,FTaskClassName,FScore from dbo.t_cdTaskClass ");
            strSql.Append(" where FTaskClassCd=@FTaskClassCd ");
            SqlParameter[] parameters = {
					new SqlParameter("@FTaskClassCd", SqlDbType.NVarChar,3)			};
            parameters[0].Value = FTaskClassCd;

            ElingOA6_Admin_DSJCJ.Model.CdTaskClass model = new ElingOA6_Admin_DSJCJ.Model.CdTaskClass();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_Admin_DSJCJ.Model.CdTaskClass DataRowToModel(DataRow row)
        {
            ElingOA6_Admin_DSJCJ.Model.CdTaskClass model = new ElingOA6_Admin_DSJCJ.Model.CdTaskClass();
            if (row != null)
            {
                if (row["FTaskClassCd"] != null)
                {
                    model.FTaskClassCd = row["FTaskClassCd"].ToString();
                }
                if (row["FTaskClassName"] != null)
                {
                    model.FTaskClassName = row["FTaskClassName"].ToString();
                }
                if (row["FScore"] != null && row["FScore"].ToString() != "")
                {
                    model.FScore = decimal.Parse(row["FScore"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FTaskClassCd,FTaskClassName,FScore ");
            strSql.Append(" FROM dbo.t_cdTaskClass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FTaskClassCd,FTaskClassName,FScore ");
            strSql.Append(" FROM dbo.t_cdTaskClass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM dbo.t_cdTaskClass ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FTaskClassCd desc");
            }
            strSql.Append(")AS Row, T.*  from dbo.t_cdTaskClass T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_cdTaskClass";
            parameters[1].Value = "FTaskClassCd";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

